﻿
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBACProject.Application;
using RBACProject.Application.Role;
using RBACProject.Application.Role.Dto;
using RBACProject.Domian;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using RBACProject.WebApi.Filter;

namespace RBACProject.WebApi.Controllers
{
    /// <summary>
    /// 角色api控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]//权限锁
    //[AuthorizationFilterAttribute]//自定义授权过滤器
    public class RoleController : ControllerBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="irole"></param>
        public RoleController(IRoleServer irole)
        {
            Irole = irole;
        }
        /// <summary>
        /// 属性
        /// </summary>
        public IRoleServer Irole { get; }

        /// <summary>
        /// 查询全部角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleList()
        {
            return Ok(Irole.GetRoleList());
        }

        /// <summary>
        /// 用户角色设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddUserRoleInfo(UserRoleModel model)
        {
            return Ok(await Irole.AddUserRoleInfo(model));
        }

        /// <summary>
        /// 新增角色信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddRoleInfo(RoleModel model)
        {
            return Ok(await Irole.AddRoleInfo(model));
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteRoleInfo(int id)
        {
            return Ok(await Irole.DeleteRole(id));
        }

        /// <summary>
        /// 查询角色全部数据
        /// </summary>
        /// <param name="roleId">角色编号</param>
        /// <param name="userName">用户名</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleInfo(int roleId = 0, string userName = "", int pageIndex = 1, int pageSize = 2)
        {
            var result = Result.Success;
            int totalCount;
            int pageCount;
            var list = Irole.GetRoleInfo(roleId, userName, pageIndex, pageSize, out totalCount, out pageCount);
            return Ok(new { result, totalCount, pageCount, list });
        }

        /// <summary>
        /// 查询  按主键查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleQueryById(int id)
        {
            var result = Result.Success;
            var data = await Irole.RoleQueryById(id);
            return Ok(new { result, data });
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdRoleInfo(RoleModel model)
        {
            return Ok(await Irole.UpdRoleInfo(model));
        }
    }    
}
